#!/usr/bin/env python
# -*- coding: utf-8 -*-

from flask import jsonify, request
from flask_restful import Resource
from utility.odoo_manager import OdooManager  # 操作odoo的单例
from utility.error_code import error_code  # 异常代码
from utility.message_manager import MessageBusManager
from event import event_type
from command import command
import logging
from utility import tools

FORMAT = "%(asctime)s %(thread)d %(message)s"
logging.basicConfig(level=logging.INFO, format=FORMAT)
TMP_MAP={'1': 'one','2': 'five','3': 'ten'}

class NewbornRecordAddAPI(Resource):
    """
    新增新生儿记录单
    :request: post
    :param archivesId: 档案ID
    :param cfqk: 产妇情况
            {
                "mqxm": 母亲姓名
                "mqage": 母亲年龄
                "fqxm": 父亲姓名
                "addr": 联系地址
                "yzZ": 孕周周
                "yzT": 孕周天
                "yc": 孕次
                "cc": 产次
                "ts": 胎数
                "cb": 产别
                "fmfs": 分娩方式
                "pgcLx": 剖宫产类型
                "jszCode": 接生者编码
                "jszName": 接生者姓名
                "ckysCode": 产科医生编码
                "ckysName": 产科医生姓名
            }
    :param xse: 新生儿
            [
                {
                    "cssj": 出生时间
                    "sex": 性别
                    "jj": 结局 活产 死胎 死产
                    "xseswSj": 新生儿死亡时间
                    "xseswYy": 新生儿死亡原因
                    "xseswYyQt": 新生儿死亡原因其他
                    "sfcsqx": 是否新生儿出生缺陷
                    "qxlb": 缺陷类别
                    "xkswt": 胸卡手腕条
                    "dyyw": 滴眼药物
                    "ysqk": 羊水情况
                    "ysl": 羊水量
                }
            ]
    :param xsetgjc: 新生儿体格检查
            {
                "length": 身长
                "weigth": 体重
                "headCircumference": 头围
                "chestCircumference": 胸围
                "skin": 皮肤情况
                "head": 头部情况
                "xsrCl": 产瘤
                "xsrXz": 血肿
                "xsrWszq": 外生殖器
                "xsrGm": 肛门
                "xsrQb": 脐部
                "xsrSz": 四肢
                "xsrHyd": 活跃度
                "xsrQd": 脐带长度
                "xsrQl": 前卤
                "xsrZl": 张力
                "xsrTw": 头围
                "xsrrj": 乳结
                "xsrXin": 心
                "xsrGan": 肝
                "xsrPi": 脾
                "xsrFei": 肺
            }
    :param aspf: 阿氏评分
            [
                {
                    "type": 类型
                    "hx": 呼吸
                    "fs": 肤色
                    "jzl": 肌张力
                    "hfs": 喉反射
                    "xt": 心跳
                    "zf": 总分
                }
            ]
    :param qjjl: 抢救记录
            {
                "sfqj": 是否抢救
                "qjcs": 抢救措施
                "xckyty": 吸出口咽痰液
                "qgcgcs": 气管插管次数
                "jygysj": 加压给氧时间
                "xysj": 吸氧时间
                "yy": 用药
            }
    :param qt: 
            {
                "xsekysCode": 新生儿科医生编码
                "xsekysName": 新生儿科医生姓名
                "jszCode": 接生者编码
                "jszName": 接生者姓名
                "hlzCode": 护理者编码
                "hlzName": 护理者姓名
                "hdzCode": 核对者编码
                "hlzN": 核对者编码
                "syzCode": 送婴者编码
                "syzName": 送婴者姓名
                "syTime": 送婴时间
                "jyzName": 接婴者姓名
                "jyTime": 接婴时间
            }
    :returns: json string
    """
    #@AppSecurity.public_verify()
    #@AppSecurity.token_verify()
    def post(self):
        try:
            odoo = OdooManager().get_instance()
            params = tools.get_params(request)
            # 参数校验
            req_params = ['archivesId','cfqk','te']
            vrf_res = tools.required_parame_verif(req_params,params)
            if  vrf_res['code'] != 0:
                return jsonify(vrf_res)
            # 产妇情况
            cfqk_data_ = {
                'archives_id': params.get('archivesId'),
                'cf_ts': params.get('ts'),
                'cf_yz_week': tools.nullablea_dict(params.get('cfqk'), 'yzZ'),
                'cf_yz_day': tools.nullablea_dict(params.get('cfqk'), 'yzT'),
                'cf_yc': tools.nullablea_dict(params.get('cfqk'), 'yc'),
                'cf_pgc': tools.nullablea_dict(params.get('cfqk'), 'pgcLx'),
                'cf_cb': tools.nullablea_dict(params.get('cfqk'), 'cb'),
                'cf_fmfs': tools.nullablea_dict(params.get('cfqk'), 'fmfs'),
                'cf_jsz': tools.nullablea_dict(params.get('cfqk'), 'jszCode'),
                'cf_ckyis': tools.nullablea_dict(params.get('cfqk'), 'ckysCode'),
                'record_line': [],
                'hospital_id': 1
            }
            o2m_data = []
            tes = params.get('te')
            if tes:
                for te in tes:
                    item_data = {
                        'xsr_cs_time': te['xsrqk']['cssj'],
                        'xsr_xb': te['xsrqk']['sex'],
                        'xsr_jj': te['xsrqk']['jj'],
                        'xsr_sw_reason': te['xsrqk']['xseswYy'],
                        'xsr_sw_reason_other': te['xsrqk']['xseswYyQt'],
                        'xsr_qx': te['xsrqk']['sfcsqx'],
                        'xsr_qxlb': te['xsrqk']['qxlb'],
                        'xsr_xkswt': te['xsrqk']['xkswt'],
                        'xsr_dyyw': te['xsrqk']['dyyw'],
                        'xsr_ysqk': te['xsrqk']['ysqk'],
                        'xsr_ysl': te['xsrqk']['ysl'],
                        'xsr_sc': te['xsetgjc']['xsrSc'],
                        'xsr_tz': te['xsetgjc']['xsrTz'],
                        'xsr_tw': te['xsetgjc']['xsrTw'],
                        'xsr_xw': te['xsetgjc']['xsrXw'],
                        'xsr_pf': te['xsetgjc']['xsrPf'],
                        'xsr_tb': te['xsetgjc']['xsrTb'],
                        'xsr_cl': te['xsetgjc']['xsrCl'],
                        'xsr_wszq': te['xsetgjc']['xsrWszq'],
                        'xsr_gm': te['xsetgjc']['xsrGm'],
                        'xsr_qd': te['xsetgjc']['xsrQb'],
                        'xsr_sz': te['xsetgjc']['xsrSz'],
                        'xsr_hyd': te['xsetgjc']['xsrHyd'],
                        'xsr_qdcd': te['xsetgjc']['xsrQd'],
                        'xsr_ql': te['xsetgjc']['xsrQl'],
                        'xsr_zl': te['xsetgjc']['xsrZl'],
                        'xsr_tw_zd': te['xsetgjc']['xsrTwZd'],
                        'xsr_rj': te['xsetgjc']['xsrRj'],
                        'xsr_x': te['xsetgjc']['xsrX'],
                        'xsr_g': te['xsetgjc']['xsrG'],
                        'xsr_p': te['xsetgjc']['xsrP'],
                        'xsr_f': te['xsetgjc']['xsrF'],
                        'xsr_pf_one_hx': te['aspf']['xsrPfOneHx'],
                        'xsr_pf_one_fs': te['aspf']['xsrPfOneFs'],
                        'xsr_pf_one_jzl': te['aspf']['xsrPfOneJzl'],
                        'xsr_pf_one_hfs': te['aspf']['xsrPfOneHfs'],
                        'xsr_pf_one_xt': te['aspf']['xsrPfOneXt'],
                        'xsr_pf_one_fz': te['aspf']['xsrPfOneFz'],
                        'xsr_pf_five_hx': te['aspf']['xsrPfFiveHx'],
                        'xsr_pf_five_fs': te['aspf']['xsrPfFiveFs'],
                        'xsr_pf_five_jzl': te['aspf']['xsrPfFiveJzl'],
                        'xsr_pf_five_hfs': te['aspf']['xsrPfFiveHfs'],
                        'xsr_pf_five_xt': te['aspf']['xsrPfFiveXt'],
                        'xsr_pf_five_fz': te['aspf']['xsrPfFiveFz'],
                        'xsr_pf_ten_hx': te['aspf']['xsrPfTenHx'],
                        'xsr_pf_ten_fs': te['aspf']['xsrPfTenFs'],
                        'xsr_pf_ten_jzl': te['aspf']['xsrPfTenJzl'],
                        'xsr_pf_ten_hfs': te['aspf']['xsrPfTenHfs'],
                        'xsr_pf_ten_xt': te['aspf']['xsrPfTenXt'],
                        'xsr_pf_ten_fz': te['aspf']['xsrPfTenFz'],
                        'xsr_qjzl': te['qjjl']['xsrQjzl'],
                        'xsr_qjcs': te['qjjl']['xsrQjcs'],
                        'xsr_xckyny': te['qjjl']['xsrXckyny'],
                        'xsr_qgcg': te['qjjl']['xsrQgcg'],
                        'xsr_jygy': te['qjjl']['xsrJygy'],
                        'xsr_xy': te['qjjl']['xsrXy'],
                        'xsr_yy': te['qjjl']['xsrYy'],
                        'xsr_ekys': te['qt']['xsekysCode'],
                        'xsr_jsz': te['qt']['jszCode'],
                        'xsr_hlz': te['qt']['hlzCode'],
                        'xsr_hdz': te['qt']['hdzCode'],
                        'xsr_syz': te['qt']['syzCode'],
                        'xsr_sysj': te['qt']['syTime'],
                        'xsr_jyz': te['qt']['jyzCode'],
                        'xsr_jysj': te['qt']['jyTime'],
                    }
                    # 'xsr_sw_time': te['xsrqk']['xseswSj'],
                    if te['xsrqk']['xseswSj']:
                        item_data['xsr_sw_time'] = te['xsrqk']['xseswSj']
                    o2m_data.append((0, 0, item_data))
            cfqk_data_['record_line'] = o2m_data
            main_id = odoo.env['birth.baby.record'].create(cfqk_data_)
            create_event = event_type.OperationLogEvent('新增新生儿记录单-产妇情况', 'id:{}'.format(main_id))
            message_bus = MessageBusManager().get_message_bus()
            message_bus.handle(create_event)  # 发布事件
            return jsonify({'code': 0, 'msg': 'success', 'data': { "id": main_id }, 'success': True})
        except AttributeError as e:
            logging.exception(e)
            return jsonify({'code': 404, 'msg': error_code[404]})
        except Exception as e:
            logging.exception(e)
            return jsonify({'code': 100, 'msg': error_code[100]})

class NewbornRecordUpdateAPI(Resource):
    """
    修改新生儿记录单
    :request: put
    :param archivesId: 档案ID
    :param cfqk: 产妇情况
            {
                "mqxm": 母亲姓名
                "mqage": 母亲年龄
                "fqxm": 父亲姓名
                "addr": 联系地址
                "yzZ": 孕周周
                "yzT": 孕周天
                "yc": 孕次
                "cc": 产次
                "ts": 胎数
                "cb": 产别
                "fmfs": 分娩方式
                "pgcLx": 剖宫产类型
                "jszCode": 接生者编码
                "jszName": 接生者姓名
                "ckysCode": 产科医生编码
                "ckysName": 产科医生姓名
            }
    :param xse: 新生儿
            [
                {
                    "cssj": 出生时间
                    "sex": 性别
                    "jj": 结局 活产 死胎 死产
                    "xseswSj": 新生儿死亡时间
                    "xseswYy": 新生儿死亡原因
                    "xseswYyQt": 新生儿死亡原因其他
                    "sfcsqx": 是否新生儿出生缺陷
                    "qxlb": 缺陷类别
                    "xkswt": 胸卡手腕条
                    "dyyw": 滴眼药物
                    "ysqk": 羊水情况
                    "ysl": 羊水量
                }
            ]
    :param xsetgjc: 新生儿体格检查
            {
                "length": 身长
                "weigth": 体重
                "headCircumference": 头围
                "chestCircumference": 胸围
                "skin": 皮肤情况
                "head": 头部情况
                "xsrCl": 产瘤
                "xsrXz": 血肿
                "xsrWszq": 外生殖器
                "xsrGm": 肛门
                "xsrQb": 脐部
                "xsrSz": 四肢
                "xsrHyd": 活跃度
                "xsrQd": 脐带长度
                "xsrQl": 前卤
                "xsrZl": 张力
                "xsrTw": 头围
                "xsrrj": 乳结
                "xsrXin": 心
                "xsrGan": 肝
                "xsrPi": 脾
                "xsrFei": 肺
            }
    :param aspf: 阿氏评分
            [
                {
                    "type": 类型
                    "hx": 呼吸
                    "fs": 肤色
                    "jzl": 肌张力
                    "hfs": 喉反射
                    "xt": 心跳
                    "zf": 总分
                }
            ]
    :param qjjl: 抢救记录
            {
                "sfqj": 是否抢救
                "qjcs": 抢救措施
                "xckyty": 吸出口咽痰液
                "qgcgcs": 气管插管次数
                "jygysj": 加压给氧时间
                "xysj": 吸氧时间
                "yy": 用药
            }
    :param qt: 
            {
                "xsekysCode": 新生儿科医生编码
                "xsekysName": 新生儿科医生姓名
                "jszCode": 接生者编码
                "jszName": 接生者姓名
                "hlzCode": 护理者编码
                "hlzName": 护理者姓名
                "hdzCode": 核对者编码
                "hlzN": 核对者编码
                "syzCode": 送婴者编码
                "syzName": 送婴者姓名
                "syTime": 送婴时间
                "jyzName": 接婴者姓名
                "jyTime": 接婴时间
            }
    :returns: json string
    """
    #@AppSecurity.public_verify()
    #@AppSecurity.token_verify()
    def put(self):
        try:
            odoo = OdooManager().get_instance()
            params = request.json
            # 参数校验
            req_params = ['archivesId','cfqk','id', 'te']
            vrf_res = tools.required_parame_verif(req_params,params)
            if  vrf_res['code'] != 0:
                return jsonify(vrf_res)
            query_ = [('archives_id', '=', int(params.get('archivesId')))]
            # 修改产妇情况
            birth_baby_record = odoo.env['birth.baby.record'].search_read(query_)
            if not birth_baby_record:
                return jsonify({'code': 40005, 'msg': error_code[40005]})
            birth_baby_record = birth_baby_record.pop()
            cfqk_data_ = {
                'archives_id': params.get('archivesId'),
                'cf_ts': params.get('ts'),
                'cf_yz_week': tools.nullablea_dict(params.get('cfqk'), 'yzZ'),
                'cf_yz_day': tools.nullablea_dict(params.get('cfqk'), 'yzT'),
                'cf_yc': tools.nullablea_dict(params.get('cfqk'), 'yc'),
                'cf_pgc': tools.nullablea_dict(params.get('cfqk'), 'pgcLx'),
                'cf_cb': tools.nullablea_dict(params.get('cfqk'), 'cb'),
                'cf_fmfs': tools.nullablea_dict(params.get('cfqk'), 'fmfs'),
                'cf_jsz': tools.nullablea_dict(params.get('cfqk'), 'jszCode'),
                'cf_ckyis': tools.nullablea_dict(params.get('cfqk'), 'ckysCode'),
                'hospital_id': 1
            }
            odoo.env['birth.baby.record'].write(birth_baby_record['id'], cfqk_data_)
            create_event = event_type.OperationLogEvent('修改新生儿记录单-产妇情况', 'id:{}'.format(cfqk_data_))
            message_bus = MessageBusManager().get_message_bus()
            message_bus.handle(create_event)  # 发布事件
            # 删除旧的详情
            odoo.env['birth.baby.record.line'].unlink(birth_baby_record['record_line'])
            # 创建新的
            tes = params.get('te')
            if tes:
                for te in tes:
                    item_data = {
                        'main_id': int(birth_baby_record['id']),
                        'xsr_cs_time': te['xsrqk']['cssj'],
                        'xsr_xb': te['xsrqk']['sex'],
                        'xsr_jj': te['xsrqk']['jj'],
                        'xsr_sw_reason': te['xsrqk']['xseswYy'],
                        'xsr_sw_reason_other': te['xsrqk']['xseswYyQt'],
                        'xsr_qx': te['xsrqk']['sfcsqx'],
                        'xsr_qxlb': te['xsrqk']['qxlb'],
                        'xsr_xkswt': te['xsrqk']['xkswt'],
                        'xsr_dyyw': te['xsrqk']['dyyw'],
                        'xsr_ysqk': te['xsrqk']['ysqk'],
                        'xsr_ysl': te['xsrqk']['ysl'],
                        'xsr_sc': te['xsetgjc']['xsrSc'],
                        'xsr_tz': te['xsetgjc']['xsrTz'],
                        'xsr_tw': te['xsetgjc']['xsrTw'],
                        'xsr_xw': te['xsetgjc']['xsrXw'],
                        'xsr_pf': te['xsetgjc']['xsrPf'],
                        'xsr_tb': te['xsetgjc']['xsrTb'],
                        'xsr_cl': te['xsetgjc']['xsrCl'],
                        'xsr_wszq': te['xsetgjc']['xsrWszq'],
                        'xsr_gm': te['xsetgjc']['xsrGm'],
                        'xsr_qd': te['xsetgjc']['xsrQb'],
                        'xsr_sz': te['xsetgjc']['xsrSz'],
                        'xsr_hyd': te['xsetgjc']['xsrHyd'],
                        'xsr_qdcd': te['xsetgjc']['xsrQd'],
                        'xsr_ql': te['xsetgjc']['xsrQl'],
                        'xsr_zl': te['xsetgjc']['xsrZl'],
                        'xsr_tw_zd': te['xsetgjc']['xsrTwZd'],
                        'xsr_rj': te['xsetgjc']['xsrRj'],
                        'xsr_x': te['xsetgjc']['xsrX'],
                        'xsr_g': te['xsetgjc']['xsrG'],
                        'xsr_p': te['xsetgjc']['xsrP'],
                        'xsr_f': te['xsetgjc']['xsrF'],
                        'xsr_pf_one_hx': te['aspf']['xsrPfOneHx'],
                        'xsr_pf_one_fs': te['aspf']['xsrPfOneFs'],
                        'xsr_pf_one_jzl': te['aspf']['xsrPfOneJzl'],
                        'xsr_pf_one_hfs': te['aspf']['xsrPfOneHfs'],
                        'xsr_pf_one_xt': te['aspf']['xsrPfOneXt'],
                        'xsr_pf_one_fz': te['aspf']['xsrPfOneFz'],
                        'xsr_pf_five_hx': te['aspf']['xsrPfFiveHx'],
                        'xsr_pf_five_fs': te['aspf']['xsrPfFiveFs'],
                        'xsr_pf_five_jzl': te['aspf']['xsrPfFiveJzl'],
                        'xsr_pf_five_hfs': te['aspf']['xsrPfFiveHfs'],
                        'xsr_pf_five_xt': te['aspf']['xsrPfFiveXt'],
                        'xsr_pf_five_fz': te['aspf']['xsrPfFiveFz'],
                        'xsr_pf_ten_hx': te['aspf']['xsrPfTenHx'],
                        'xsr_pf_ten_fs': te['aspf']['xsrPfTenFs'],
                        'xsr_pf_ten_jzl': te['aspf']['xsrPfTenJzl'],
                        'xsr_pf_ten_hfs': te['aspf']['xsrPfTenHfs'],
                        'xsr_pf_ten_xt': te['aspf']['xsrPfTenXt'],
                        'xsr_pf_ten_fz': te['aspf']['xsrPfTenFz'],
                        'xsr_qjzl': te['qjjl']['xsrQjzl'],
                        'xsr_qjcs': te['qjjl']['xsrQjcs'],
                        'xsr_xckyny': te['qjjl']['xsrXckyny'],
                        'xsr_qgcg': te['qjjl']['xsrQgcg'],
                        'xsr_jygy': te['qjjl']['xsrJygy'],
                        'xsr_xy': te['qjjl']['xsrXy'],
                        'xsr_yy': te['qjjl']['xsrYy'],
                        'xsr_ekys': te['qt']['xsekysCode'],
                        'xsr_jsz': te['qt']['jszCode'],
                        'xsr_hlz': te['qt']['hlzCode'],
                        'xsr_hdz': te['qt']['hdzCode'],
                        'xsr_syz': te['qt']['syzCode'],
                        'xsr_sysj': te['qt']['syTime'],
                        'xsr_jyz': te['qt']['jyzCode'],
                        'xsr_jysj': te['qt']['jyTime'],
                    }
                    # 'xsr_sw_time': te['xsrqk']['xseswSj'],
                    if te['xsrqk']['xseswSj']:
                        item_data['xsr_sw_time'] = te['xsrqk']['xseswSj']
                    odoo.env['birth.baby.record.line'].create(item_data)
            return jsonify({'code': 0, 'msg': 'success', 'data': {}, 'success': True})
        except AttributeError as e:
            logging.exception(e)
            return jsonify({'code': 404, 'msg': error_code[404]})
        except Exception as e:
            logging.exception(e)
            return jsonify({'code': 100, 'msg': error_code[100]})

class NewbornRecordListAPI(Resource):
    """
    回显新生儿记录单
    :request: get
    :param archivesId: 档案ID
    :returns: json string
    """

    #@AppSecurity.public_verify()
    #@AppSecurity.token_verify()
    def get(self):

        try:
            #  参数校验
            params = request.values
            req_params = ['archivesId', 'id']
            vrf_res = tools.required_parame_verif(req_params,params)
            if  vrf_res['code'] != 0:
                return jsonify(vrf_res)
            #  获取产妇情况
            odoo = OdooManager().get_instance()
            #  构造查询条件
            res_archives_ = odoo.env['res.archives'].search_read([('id', '=', int(params.get('archivesId')))])
            if (res_archives_):
                res_archives_ = res_archives_.pop()
            birth_baby_record_ = odoo.env['birth.baby.record'].search_read([('archives_id', '=', int(params.get('archivesId'))), ('id', '=', int(params.get('id')))])
            if birth_baby_record_:
                birth_baby_record_ = birth_baby_record_.pop()
            else:
                return {
                    "msg": 'success',
                    'code': 0,
                    'data': {}
                }
            data_ = {
                'te': [],
                'id': birth_baby_record_['id'],
                'cfqk': {},
                'ts': birth_baby_record_['cf_ts']
            }
            #  产妇情况
            data_['cfqk'] = {
                'mqXm': res_archives_['mq_xm'],
                'mqAge': res_archives_['mq_age'],
                'fqXm': res_archives_['fq_xm'],
                'mqZz': res_archives_['mq_cz_address'],
                'yzZ': birth_baby_record_['cf_yz_week'],
                'yzT': birth_baby_record_['cf_yz_day'],
                'yc': birth_baby_record_['cf_yc'],
                'ts': birth_baby_record_['cf_ts'],
                'cb': birth_baby_record_['cf_cb'],
                'fmfs': birth_baby_record_['cf_fmfs'],
                'pgcLx': birth_baby_record_['cf_pgc'],
                'jszCode': birth_baby_record_['cf_jsz'][0] if birth_baby_record_['cf_jsz'] else None,
                'jszName': birth_baby_record_['cf_jsz'][1] if birth_baby_record_['cf_jsz'] else None,
                'ckysCode': birth_baby_record_['cf_ckyis'][0] if birth_baby_record_['cf_ckyis'] else None,
                'ckysName': birth_baby_record_['cf_ckyis'][1] if birth_baby_record_['cf_ckyis'] else None
            }
            #  新生儿
            query_ = [('id', 'in', birth_baby_record_['record_line'])]
            birth_baby_record_line = odoo.env['birth.baby.record.line'].search_read(query_)
            if birth_baby_record_line:
                for line_ in birth_baby_record_line:
                    te = {
                        'xsrqk': {
                            'cssj': line_['xsr_cs_time'],
                            'sex': line_['xsr_xb'],
                            'jj': line_['xsr_jj'],
                            'xseswSj': line_['xsr_sw_time'],
                            'xseswYy': line_['xsr_sw_reason'],
                            'xseswYyQt': line_['xsr_sw_reason_other'],
                            'sfcsqx': line_['xsr_qx'],
                            'qxlb': line_['xsr_qxlb'],
                            'xkswt': line_['xsr_xkswt'],
                            'dyyw': line_['xsr_dyyw'],
                            'ysqk': line_['xsr_ysqk'],
                            'ysl': line_['xsr_ysl'],
                        },
                        'xsetgjc': {
                            'xsrSc': line_['xsr_sc'],
                            'xsrTz': line_['xsr_tz'],
                            'xsrTw': line_['xsr_tw'],
                            'xsrXw': line_['xsr_xw'],
                            'xsrPf': line_['xsr_pf'],
                            'xsrTb': line_['xsr_tb'],
                            'xsrCl': line_['xsr_cl'],
                            'xsrWszq': line_['xsr_wszq'],
                            'xsrGm': line_['xsr_gm'],
                            'xsrQb': line_['xsr_qd'],
                            'xsrSz': line_['xsr_sz'],
                            'xsrHyd': line_['xsr_hyd'],
                            'xsrQd': line_['xsr_qdcd'],
                            'xsrQl': line_['xsr_ql'],
                            'xsrZl': line_['xsr_zl'],
                            'xsrTwZd': line_['xsr_tw_zd'],
                            'xsrrj': line_['xsr_rj'],
                            'xsrX': line_['xsr_x'],
                            'xsrG': line_['xsr_g'],
                            'xsrP': line_['xsr_p'],
                            'xsrF': line_['xsr_f']
                        },
                        'qjjl': {
                            'xsrQjzl': line_['xsr_qjzl'],
                            'xsrQjcs': line_['xsr_qjcs'],
                            'xsrXckyny': line_['xsr_xckyny'],
                            'xsrQgcg': line_['xsr_qgcg'],
                            'xsrJygy': line_['xsr_jygy'],
                            'xsrXy': line_['xsr_xy'],
                            'xsrYy': line_['xsr_yy'],
                        },
                        'qt': {
                            'syzCode': line_['xsr_syz'][0] if line_['xsr_syz'] else None,
                            'syTime': line_['xsr_sysj'],
                            'jyTime': line_['xsr_jysj'],
                            'jyzCode': line_['xsr_jyz'][0] if line_['xsr_jyz'] else None,
                            'hdzCode': line_['xsr_hdz'][0] if line_['xsr_hdz'] else None,
                            'hlzCode': line_['xsr_hlz'][0] if line_['xsr_hlz'] else None,
                            'xsekysCode': line_['xsr_ekys'][0] if line_['xsr_ekys'] else None,
                            'jszCode': line_['xsr_jsz'][0] if line_['xsr_jsz'] else None
                        },
                        'aspf':{
                            'xsrPfOneHx': line_['xsr_pf_one_hx'],
                            'xsrPfOneJzl': line_['xsr_pf_one_jzl'],
                            'xsrPfOneFs': line_['xsr_pf_one_fs'],
                            'xsrPfOneHfs': line_['xsr_pf_one_hfs'],
                            'xsrPfOneXt': line_['xsr_pf_one_xt'],
                            'xsrPfOneFz': line_['xsr_pf_one_fz'],
                            'xsrPfFiveHx': line_['xsr_pf_five_hx'],
                            'xsrPfFiveJzl': line_['xsr_pf_five_jzl'],
                            'xsrPfFiveFs': line_['xsr_pf_five_fs'],
                            'xsrPfFiveHfs': line_['xsr_pf_five_hfs'],
                            'xsrPfFiveXt': line_['xsr_pf_five_xt'],
                            'xsrPfFiveFz': line_['xsr_pf_five_fz'],
                            'xsrPfTenHx': line_['xsr_pf_ten_hx'],
                            'xsrPfTenJzl': line_['xsr_pf_ten_jzl'],
                            'xsrPfTenFs': line_['xsr_pf_ten_fs'],
                            'xsrPfTenHfs': line_['xsr_pf_ten_hfs'],
                            'xsrPfTenXt': line_['xsr_pf_ten_xt'],
                            'xsrPfTenFz': line_['xsr_pf_ten_fz']
                        }
                    }
                    data_['te'].append(te)
            return jsonify({'code': 0, 'msg': 'success', 'data': data_, 'success': True})
        except AttributeError as e:
            logging.exception(e)
            return jsonify({'code': 404, 'msg': error_code[404]})
        except Exception as e:
            logging.exception(e)
            return jsonify({'code': 100, 'msg': error_code[100]})



